package com.hylanda.processors.getdomainname.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author shijinxiang
 * @date 2017年12月27日
 * @time 下午4:14:27
 */
public class URLHelper {
	
	@SuppressWarnings("serial")
	public static final List<String> excludeUrl=new ArrayList<String>(){
		{
			add("xhpfmapi.zhongguowangshi.com/share/index.html");
		}
	};
	
	@SuppressWarnings("serial")
	public static final List<String> urlsub=new ArrayList<String>(){
		{
			add(".html");
			add(".xhtml");
			add(".shtml");
			add(".htm");
		}
	};
//	public static void main(String[] args) {
//		regular("http://xhpfmapi.zhongguowangshi.com/share/index.html?docid=2337533&channel=weixin");
//	}
	
	public static String regular(String url){
		for(String u:excludeUrl){
			if(url.contains(u))
				return url;
		}
		for(String s:urlsub){
			if(url.contains(s)){
				url=url.substring(0, url.indexOf(s)+s.length());
				break;
			}
		}
		return url;
	}
	
	public static boolean isPicOrJSOrCSS(String url)
	{
		boolean flag = false;
		if (url.contains(".css")) {
			flag = true;
		}else if(url.contains(".js"))
		{
			flag = true;
		}else if(url.contains(".png"))
		{
			flag = true;
		}else if(url.contains(".gif"))
		{
			flag = true;
		}else if(url.contains(".jpg"))
		{
			flag = true;
		}else if(url.contains(".gif"))
		{
			flag = true;
		}else if(url.contains(".flash"))
		{
			flag = true;
		}else if(url.contains(".mp4"))
		{
			flag = true;
		}else if(url.contains(".avi"))
		{
			flag = true;
		}else if(url.contains(".mkv"))
		{
			flag = true;
		}
		return flag ;
	}
	public static String decode(String url){
		if(url==null)
			return null;
		try {
			url = url.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
			url = url.replaceAll("\\+", "%2B");
			url=URLDecoder.decode(url,"UTF-8");
		} catch (Exception e) {
		}
		return url;
	}
	
	public static String encode(String url){
		if(url==null)
			return null;
		try {
			url=URLEncoder.encode(url,"UTF-8");
		} catch (Exception e) {
		}
		return url;
	}
}
